<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <h2>Vue - 实现留言板 - todoList</h2>
        <div>
            <h3>显示留言</h3>
            <div v-for="(l,i) in list">
                {{i}}.{{l.title}}
                <button v-on:click="deleteOne(i)">删除</button>
                <button v-on:click="updateOne(l,i)">修改</button>
            </div>
        </div>
        <h3>新增留言</h3>
        <p>
            <input type="text" placeholder="请输入" v-model="text">
            <button v-on:click="addToList">添加</button>
        </p>
    </div>
</body>
<script>
    const vm = new Vue({
        el: "#app",
        data: {
            text: "",
            list: [
                {
                    title: "Vue so easy"
                },
                {
                    title: "javascript is so beautiful"
                },
                {
                    title: "jQuery is 如此好用"
                },
                {
                    title: "css永远的神"
                }
            ]
        },
        methods: {
            updateOne(l, i) {
                var value = window.prompt(i, l.title);
                console.log(value);
                if (value) {
                    l.title = value
                    this.list.splice(i, 1, l)
                }
            },
            deleteOne(i) {
                this.list.splice(i, 1)
            },
            addToList() {
                if (this.text) {
                    this.list.push({
                        title: this.text
                    })
                    this.text = ""
                } else {
                    alert("留言内容不能为空");
                }
            }
        }
    })
</script>

</html>